﻿//============================================================================
// Name        : rh_autoLogin
// Author      : Chaiyaphum Siripanpornchana
// Version     : 1.0
// Description : KKU Internet Access Authen auto login
//============================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Runtime.InteropServices;
using System.IO;

namespace rh_autoLogin
{
    class Program
    {
        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int conn, int val);

        static void Main(string[] args)
        {
            int Out;
            string user_name = "5330404415";
            string passwd = "14112534";

            if (InternetGetConnectedState(out Out, 0) == true) {
                // If user connect internet
                Console.WriteLine("[+] You are connected to the internet !");

                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.google.com");
                HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

                if (myHttpWebResponse.Server.ToString() == "gws") {
                    // login
                    Console.WriteLine("[+] KKU Internet Access Authen...Login succeed");
                } else {
                    // User not login
                    Console.WriteLine("[+] Try to login KKU Internet Access Authen");

                    WebRequest request = WebRequest.Create("http://rahunas-04/login.php");
                    request.Method = "POST";

                    string postData = "user=" + user_name + "&passwd=" + passwd;
                    byte[] byteArray = Encoding.UTF8.GetBytes(postData);

                    request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentLength = byteArray.Length;

                    Stream dataStream = request.GetRequestStream();
                    dataStream.Write(byteArray, 0, byteArray.Length);
                    dataStream.Close();

                    WebResponse response = request.GetResponse();

                    if (((HttpWebResponse)response).StatusCode.ToString().Equals("OK")) {
                        Console.WriteLine("[+] KKU Internet Access Authen...Login succeed");
                    } else {
                        Console.WriteLine("[+] KKU Internet Access Authen...Login unsuccessful");
                    }
                }

                myHttpWebResponse.Close();

            } else {
                // If user not connect internet
                Console.WriteLine("[+] You are not connected to the internet !");
            }

            Console.WriteLine("[+] Press Any Key to exit...");
            Console.ReadKey(true);
        }

    }
}
